const router = require('koa-router')()
const mysql= require('../../db')
router.prefix('/before')
// 根据id查询环保类型
router.get('/protect/:pid', async (ctx, next) => {
  try {
    let {pid} = ctx.params;
    let sql = "select * from protect where pr_id=?";
    let cars = await mysql(sql, [pid]);
    ctx.body = {
      status: 1,
      message: cars
    }
  } catch (error) {
    ctx.body = {
      status: 0,
      message: error
    }
  } 
})
// 根据id查询能源
router.get('/energy/:eid', async (ctx, next) => {
    try {
      let {eid} = ctx.params;
      let sql = "select * from energy where en_id=?";
      let cars = await mysql(sql, [eid]);
      ctx.body = {
        status: 1,
        message: cars
      }
    } catch (error) {
  
      ctx.body = {
        status: 0,
        message: error
      }
    } 
})

// 根据id查询发布人
router.get('/admin/:aid', async (ctx, next) => {
  try {
    let {aid} = ctx.params;
    let sql = "select * from admin where a_id=?";
    let cars = await mysql(sql, [aid]);
    ctx.body = {
      status: 1,
      message: cars
    }
  } catch (error) {
    ctx.body = {
      status: 0,
      message: error
    }
  } 
})
// 根据id查询查询车型
router.get('/classify/:cid', async (ctx, next) => {
  try {
    let {cid} = ctx.params;
    let sql = "select * from classify where c_id=?";
    let cars = await mysql(sql, [cid]);
    ctx.body = {
      status: 1,
      message: cars
    }

  } catch (error) {

    ctx.body = {
      status: 0,
      message: error
    }
  } 
})
// 根据id查询厂商
router.get('/manufacturer/:mid', async (ctx, next) => {
  try {
    let {mid} = ctx.params;
    let sql = "select * from manufacturer where m_id=?";
    let cars = await mysql(sql, [mid]);
    ctx.body = {
      status: 1,
      message: cars
    }

  } catch (error) {

    ctx.body = {
      status: 0,
      message: error
    }
  } 
})

// 轮播图
router.get('/imgs',async function (ctx, next) {
  try {
    let sql = "select ca_id,ca_img,ca_state,ca_p_id from carousel where ca_state=1";
    let result = await mysql(sql,[]);
    ctx.body = {
      status:1,
      message:result
    }
    
  } catch (error) {
    next(error);
  }
})
//分页显示所有的产品
router.get("/product/:pageNum",async (ctx,next)=>{
	try {
		let {pageNum} = ctx.params;
		
		 let sql = "select count(*) num from product";
		 let num = await mysql(sql,[]);
		 num = num[0].num;
		 let pageSize = 6;
		 let start = (pageNum-1)*pageSize;
		 sql = "select * from product where p_status=1 limit ?,?";     
		let result = await mysql(sql,[start,pageSize]);
		let totalPage = num % pageSize == 0 ? parseInt(num/pageSize) : parseInt(num/pageSize)+1;
      ctx.body = {
        status:1,
        message:result,
		    totalPage:totalPage
      }
      
    } catch (error) {
      next(error);
    }
});


//根据商品id查询所有的信息
router.get("/productinfo/:pid",async (ctx,next)=>{
	try {
		let {pid} = ctx.params;		
		let sql = "select * from product where p_id = ?";
		let result = await mysql(sql,[pid]);
      ctx.body = {
        status:1,
        message:result
      }
      
    } catch (error) {
      next(error);
    }
});
//从购物车查询所有未结算的数据
router.get("/shopcar",async (ctx,next)=>{
	try {
			
		let sql = "select * from shopcar where c_status=1";
		let result = await mysql(sql,[]);
      ctx.body = {
        status:1,
        message:result
      }
      
    } catch (error) {
      next(error);
    }
});

//添加到购物车中
router.post("/addshopcar",async (ctx, next)=>{
	try {
    let {c_p_id,p_price,c_num,c_u_id,p_name,p_img} = ctx.request.body;
    let sql = "insert into shopcar(c_p_id,p_price,c_num,c_u_id,p_name,p_img) values(?,?,?,?,?,?)";
    let result = await mysql(sql, [c_p_id, p_price, c_num, c_u_id, p_name, p_img]);
    sql = `update product set p_number=p_number-?,p_sellNumber=p_sellNumber+? where p_id=?`;
    await mysql(sql, [c_num,c_num,c_p_id]);
      ctx.body = {
        status:1,
        message:result
      }
  } catch (error) {
    next(error);
  }
	
});
//修改到购物车
router.put("/updateshopcar",async (ctx, next)=>{
	try {   
    let {c_num,c_status,c_id} = ctx.request.body;
    let sql = "update shopcar set c_num = ?,c_selected=? where c_id = ?";
    let result = await mysql(sql,[c_num,c_status,c_id]);
      ctx.body = {
        status:1,
        message:result
      }
  } catch (error) {
    next(error);
  }
	
});

//删除购物车某个商品
router.delete('/deleteshopcar', async (ctx, next) => {
 try {
  let {c_id,c_num} = ctx.query;
  let sql = "update shopcar set c_status=0 where c_id = ?";
  let result = mysql(sql, [c_id]);
  sql = `update product set p_number=p_number+?,p_sellNumber=p_sellNumber-? where p_id=?`;
  await mysql(sql, [c_num,c_num,c_id]);
  ctx.body = {
    status:1,
    message:result
  }
 } catch (error) {
   next(error);
 }
})

// 查询所有的汽车图片
router.get("/imgcar/:id",async (ctx,next)=>{
	try {
		let {id}=ctx.params
		let sql = "select * from imgcar  where img_p_id=?";
		let result = await mysql(sql,[id]);
      ctx.body = {
        status:1,
        message:result
      }
      
    } catch (error) {
      next(error);
    }
});

//  商品评价表 evaluate
router.post("/addevaluate", async (ctx, next) => {
	try {   
    let {e_content,e_date,e_p_id,e_u_id} = ctx.request.body;
    let sql = "insert into evaluate(e_content,e_date,e_p_id,e_u_id) values(?,?,?,?)";
    let result = await mysql(sql,[e_content,e_date,e_p_id,e_u_id]);
	console.log(result);
      ctx.body = {
        status:1,
        message:result
      }
    
  } catch (error) {
    next(error);
  }
	
});
// 商品追评  addevaluate
router.post("/addevaluates", async (ctx, next) => {
	try {   
    let {add_content,add_img,add_date,add_p_id,add_u_id} = ctx.request.body;

    let sql = "insert into shopcar(add_content,add_img,add_date,add_p_id,add_u_id) values(?,?,?,?,?)";
    let result = await mysql(sql,[add_content,add_img,add_date,add_p_id,add_u_id]);
      ctx.body = {
        status:1,
        message:result
      }
    
  } catch (error) {
    next(error);
  }
	
});
// 添加用户 user
router.post("/adduser",async (ctx, next)=>{
  try {
    let { u_icon, u_account, u_nickname, u_password, u_gender, u_phone, u_email, u_idCard } = ctx.request.body;
    let sql = `select * from users where u_account=?`;
    res = await mysql(sql, [u_account])
    if (res.length < 0) {
      sql = "insert into users(u_icon,u_account,u_nickname,u_password,u_gender,u_phone,u_email,u_idCard) values(?,?,?,?,?,?,?,?)";
      let result = await mysql(sql,[u_icon,u_account,u_nickname,u_password,u_gender,u_phone,u_email,u_idCard]);
      ctx.body = {
        status:1,
        message:result
      }
    } else {
      ctx.body = {
        status:0,
        message:"用户已存在"
      }
    }
  } catch (error) {
    next(error);
  }
	
});

// 查询所有头像
router.get("/head",async (ctx,next)=>{
	try {
		let sql = "select * from head  ";
		let result = await mysql(sql,[]);
      ctx.body = {
        status:1,
        message:result
      }
      
    } catch (error) {
      next(error);
    }
});
// 根据id查询头像
router.get("/head/:id",async (ctx,next)=>{
  try {
    let {id}=ctx.params
		let sql = "select * from head where h_id=? ";
		let result = await mysql(sql,[id]);
      ctx.body = {
        status:1,
        message:result
      }
      
    } catch (error) {
      next(error);
    }
});

// 根据商品id分页查询商品评论

router.get('/evaluates', async (ctx, next) => {
  try {
    let { currentsize, pagesize ,e_p_id} = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
      let sql = "select evaluate.*,users.u_nickname from evaluate,users where  e_p_id= ? and evaluate.e_u_id =users.u_id limit ?,?";
    let cars = await mysql(sql, [e_p_id,indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from evaluate',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) {
      next(error);
    } 
 })
// 根据会员类型id查询会员名字
router.get('/vipkind/:vid', async (ctx, next) => {
  try {
    let {vid} = ctx.params;
    let sql = "select * from vipkind where v_id=?";
    let cars = await mysql(sql, [vid]);
    ctx.body = {
      status: 1,
      message: cars
    }

  } catch (error) {

    ctx.body = {
      status: 0,
      message: error
    }
  } 
})

// 查询所有的会员类型
router.get('/vipkind', async (ctx, next) => {
  try {
    let sql = "select * from vipkind ";
    let cars = await mysql(sql, []);
    ctx.body = {
      status: 1,
      message: cars
    }

  } catch (error) {

    ctx.body = {
      status: 0,
      message: error
    }
  } 
})

// 在用户列表添加会员
router.put("/addvip",async (ctx, next)=>{
	try {   
    let {u_v_id,u_id} = ctx.request.body;
    let sql = "update users set u_v_id = ? where u_id = ?";
    let result = await mysql(sql,[u_v_id,u_id]);
      ctx.body = {
        status:1,
        message:result
      }
  } catch (error) {
    next(error);
  }
	
});

// 生成订单
router.post("/addordertable",async (ctx, next)=>{
	try {   
    let { o_serial, o_money, o_date, o_address, o_p_id, o_u_id, o_comment } = ctx.request.body;
    if (o_comment == null) {
      o_comment ='无备注'
    }
    let sql = "insert into ordertable(o_serial,o_money,o_date,o_address,o_comment,o_p_id,o_u_id) values(?,?,?,?,?,?,?)";
    let result = await mysql(sql,[o_serial,o_money,o_date,o_address,o_comment,o_p_id,o_u_id]);
      ctx.body = {
        status:1,
        message:result
      }
  } catch (error) {
    next(error);
  }	
});


// 根据用户id查询订单
router.get('/ordertable/:uid', async (ctx, next) => {
  try {
    let { uid } = ctx.params;
    let sql = "select o_id,o_serial,o_date,o_money,o_address,o_comment,o_p_id,o_u_id,(case when o_status=1 then '未发货' else (case when o_status=0 then '发货中' else '已收货' end) end) o_status from ordertable  where o_u_id=?";
    let cars = await mysql(sql, [uid]);
    ctx.body = {
      status: 1,
      message: cars
    }
  } catch (error) {
    next(error);
  } 
})

// 订单提交后修改金额
router.put("/users",async (ctx, next)=>{
  try {
    let { u_money, u_id } = ctx.request.body;
    let sql = "select u_money from users where u_id=?"
    let result = await mysql(sql, [u_id]);
    u_money = parseFloat(u_money)
    result[0].u_money = parseFloat(result[0].u_money)
    console.log(result[0].u_money > u_money);
    if (result[0].u_money > u_money) {
      
      sql = "update users set u_money=? where u_id = ?";
     result = await mysql(sql,[u_money,u_id]);
      ctx.body = {
        status:1,
        message:result
      }
    } else {
      ctx.body = {
        status:0,
        message:"余额不足"
      }
    }
    
  } catch (error) {
    next(error);
  }
	
});

// 分页查询所有的视频
router.get("/video/:pageNum",async (ctx,next)=>{
	try {
		let {pageNum} = ctx.params;
		
		 let sql = "select count(*) num from video";
		 let num = await mysql(sql,[]);
		 num = num[0].num;
		 let pageSize = 6;
		 let start = (pageNum-1)*pageSize;
		 sql = "select v_id, v_data,v_name ,v_number,v_pepole,v_content from video order by v_id desc limit ?,?";     
		let result = await mysql(sql,[start,pageSize]);
		let totalPage = num % pageSize == 0 ? parseInt(num/pageSize) : parseInt(num/pageSize)+1;
      ctx.body = {
        status:1,
        message:result,
		    totalPage:totalPage
      }
      
    } catch (error) {
      next(error);
    }
});


// 公告
router.get("/news/:pageNum",async (ctx,next)=>{
	try {
		let {pageNum} = ctx.params;
		
		 let sql = "select count(*) num from news";
		 let num = await mysql(sql,[]);
		 num = num[0].num;
		 let pageSize = 6;
		 let start = (pageNum-1)*pageSize;
		 sql = "select newsId, newsDate,newsContent,readNum,newsTitle,praise,n_a_id from news order by newsId desc limit ?,?";     
		let result = await mysql(sql,[start,pageSize]);
		let totalPage = num % pageSize == 0 ? parseInt(num/pageSize) : parseInt(num/pageSize)+1;
      ctx.body = {
        status:1,
        message:result,
		    totalPage:totalPage
      }
      
    } catch (error) {
      next(error);
    }
});
// 根据id修改订单状态
router.put("/ordertables",async (ctx, next)=>{
	try {   
    let {o_status,o_id} = ctx.request.body;
    let sql = "update ordertable set o_status=? where o_id=?";
    let result = await mysql(sql,[o_status,o_id]);
      ctx.body = {
        status:1,
        message:result
      }
  } catch (error) {
    next(error);
  }
	
});

// 根据id查询折扣
router.get('/uvinpkind/:vid', async (ctx, next) => {
  try {
    let { vid } = ctx.params;
    let sql = "select v.v_name,v.v_discount  from users u left join vipkind v  on   u.u_v_id=v.v_id  where u.u_id =?";
    let cars = await mysql(sql, [vid]);
    ctx.body = {
      status: 1,
      message: cars
    }
  } catch (error) {
    next(error);
  } 
})
//点击某个公告  打开的同时让浏览器数量加1
router.get('/findNews/:newsId',async function (ctx, next) {
  try {
    
    let {newsId} = ctx.params;
    let sql = "update news set readNum = readNum+ 1 where newsId = ?";
    let result = await mysql(sql,[newsId]);
    sql = "select n.newsDate,n.newsTitle ,n.readNum, n.newsContent,a.a_name from news n left join  admin a on  n.n_a_id=a.a_id  where newsId =?";
    result = await mysql(sql,[newsId]);
      ctx.body = {
        status:1,
        message:result
      }
    
  } catch (error) {
    next(error);
  }
  
})
module.exports = router